தொகுப்பு அறிமுகம்
MongoDB இல் உள்ள ஒரு தொகுப்பு MySQL இல் உள்ள அட்டவணைக்கு சமமானதாகும்
MySQL அட்டவணை
- நிர்ணயிக்கப்பட்ட கட்டமைப்பு
- நெடுவரிசைகள் மற்றும் வரிசைகள்
- வரையறுக்கப்பட்ட தரவு வகைகள்
- முன்கூட்டியே வரையறுக்கப்பட்டது
MongoDB தொகுப்பு
- நெகிழ்வான கட்டமைப்பு
- ஆவணங்கள் (JSON போன்றது)
- வெவ்வேறு தரவு வகைகள்
- தானாக உருவாக்கப்படும்
தொகுப்பை உருவாக்குதல்
MongoDB இல் ஒரு தொகுப்பை உருவாக்க, createCollection() முறையைப் பயன்படுத்தவும்:
எடுத்துக்காட்டு
"customers" என்ற தொகுப்பை உருவாக்கவும்:
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db("mydb");
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
மேலே உள்ள குறியீட்டை "demo_mongodb_createcollection.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_mongodb_createcollection.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
Collection created!
முக்கியமான குறிப்பு:
MongoDB இல், உள்ளடக்கம் கிடைக்கும் வரை ஒரு தொகுப்பு உருவாக்கப்படாது!
தொகுப்பை உண்மையில் உருவாக்குவதற்கு முன்பு, MongoDB நீங்கள் ஒரு ஆவணத்தைச் செருகும் வரை காத்திருக்கும்.
நவீன தொகுப்பு உருவாக்கம்
நவீன async/await தொடரியல்
const { MongoClient } = require('mongodb');
async function createCollectionModern() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create collection explicitly
const collection = await database.createCollection("customers");
console.log("Collection created successfully:", collection.collectionName);
} catch (err) {
console.error("Error creating collection:", err);
} finally {
await client.close();
}
}
createCollectionModern();
தானாக தொகுப்பு உருவாக்கம்
const { MongoClient } = require('mongodb');
async function autoCreateCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Collection will be created automatically on first insert
const collection = database.collection("customers");
// Insert first document - this creates the collection
const result = await collection.insertOne({
name: "John Doe",
email: "john@example.com",
createdAt: new Date()
});
console.log("Collection created automatically with first document");
console.log("Document inserted with id:", result.insertedId);
} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}
autoCreateCollection();
தொகுப்பு விருப்பங்கள்
சரிசெய்த தொகுப்பு உருவாக்கம்
const { MongoClient } = require('mongodb');
async function createCappedCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create a capped collection (fixed size)
const collection = await database.createCollection("logs", {
capped: true,
size: 1000000, // 1MB in bytes
max: 1000 // Maximum 1000 documents
});
console.log("Capped collection created successfully");
} catch (err) {
console.error("Error creating capped collection:", err);
} finally {
await client.close();
}
}
createCappedCollection();
சரிபார்ப்பு விதிகளுடன் தொகுப்பு
const { MongoClient } = require('mongodb');
async function createValidatedCollection() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
// Create collection with validation rules
const collection = await database.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType: "string",
pattern: "^.+@.+\\..+$",
description: "must be a valid email and is required"
},
age: {
bsonType: "int",
minimum: 0,
maximum: 150,
description: "must be an integer between 0 and 150"
}
}
}
}
});
console.log("Validated collection created successfully");
} catch (err) {
console.error("Error creating validated collection:", err);
} finally {
await client.close();
}
}
createValidatedCollection();
தொகுப்பு நிர்வாகம்
தொகுப்புகளைப் பட்டியலிடுதல்
const { MongoClient } = require('mongodb');
async function listCollections() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
const collections = await database.listCollections().toArray();
console.log("Collections in database:");
collections.forEach(collection => {
console.log(` - ${collection.name}`);
});
} catch (err) {
console.error("Error listing collections:", err);
} finally {
await client.close();
}
}
listCollections();
தொகுப்பு இருப்பைச் சரிபார்க்கவும்
const { MongoClient } = require('mongodb');
async function checkCollectionExists() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("mydb");
const collections = await database.listCollections().toArray();
const exists = collections.some(col => col.name === "customers");
if (exists) {
console.log("Collection 'customers' exists");
} else {
console.log("Collection 'customers' does not exist");
}
} catch (err) {
console.error("Error checking collection:", err);
} finally {
await client.close();
}
}
checkCollectionExists();
தொகுப்பு வகைகள் மற்றும் பயன்பாட்டு நிகழ்வுகள்
| தொகுப்பு வகை | விளக்கம் | பயன்பாட்டு நிகழ்வுகள் |
|---|---|---|
| இயல்பான தொகுப்பு | கட்டுப்பாடுகள் இல்லாத நெகிழ்வான தொகுப்பு | பயனர் தரவு, தயாரிப்புகள், உத்தியோகபூர்வ தரவு |
| சரிசெய்த தொகுப்பு | நிலையான அளவு கொண்ட தொகுப்பு | பதிவுகள், அடுக்கு தரவு, உணரி வாசிப்புகள் |
| சரிபார்க்கப்பட்ட தொகுப்பு | தரவு சரிபார்ப்பு விதிகள் கொண்ட தொகுப்பு | முக்கியமான தரவு, பயனர் உள்ளீடு, API தரவு |
| காலவரையற்ற தொகுப்பு | காலவரையின்றி தரவு சேமிக்கும் தொகுப்பு | வரலாற்று தரவு, ஆவணக் காப்பகங்கள் |
தொகுப்பு உருவாக்க சிறந்த நடைமுறைகள்
பெயரிடும் மரபுகள்
- பன்மை பெயர்களைப் பயன்படுத்தவும் (users, products)
- சிறிய எழுத்துக்களைப் பயன்படுத்தவும்
- அடிக்கோடுகளைத் தவிர்க்கவும்
- விவரிக்கும் பெயர்களைப் பயன்படுத்தவும்
கட்டமைப்பு உத்திகள்
- தேவைக்கேற்ப வெவ்வேறு தொகுப்புகளை உருவாக்கவும்
- தரவு அணுகல் வடிவங்களைக் கருத்தில் கொள்ளவும்
- அளவிடுதல் தேவைகளைத் திட்டமிடவும்
- குறியீட்டு உத்திகளைத் திட்டமிடவும்
தரவு ஒருமைப்பாடு
- முக்கியமான தரவுக்கு சரிபார்ப்பு விதிகளைப் பயன்படுத்தவும்
- தேவையான புலங்களைக் குறிப்பிடவும்
- தரவு வகை கட்டுப்பாடுகளை அமைக்கவும்
- தொகுப்பு அனுமதிகளை உள்ளமைக்கவும்
முழுமையான எடுத்துக்காட்டு
தொகுப்பு உருவாக்கம் மற்றும் நிர்வாகத்தின் முழுமையான எடுத்துக்காட்டு:
const { MongoClient } = require('mongodb');
class CollectionManager {
constructor(connectionString, dbName) {
this.connectionString = connectionString;
this.dbName = dbName;
this.client = new MongoClient(connectionString);
}
async connect() {
try {
await this.client.connect();
console.log("Connected to MongoDB server");
this.database = this.client.db(this.dbName);
return true;
} catch (error) {
console.error("Connection failed:", error);
return false;
}
}
async createCollection(collectionName, options = {}) {
try {
const collection = await this.database.createCollection(collectionName, options);
console.log(`Collection '${collectionName}' created successfully`);
return collection;
} catch (error) {
console.error(`Error creating collection '${collectionName}':`, error);
throw error;
}
}
async getOrCreateCollection(collectionName) {
try {
// Check if collection exists
const collections = await this.database.listCollections().toArray();
const exists = collections.some(col => col.name === collectionName);
if (exists) {
console.log(`Collection '${collectionName}' already exists`);
return this.database.collection(collectionName);
} else {
return await this.createCollection(collectionName);
}
} catch (error) {
console.error(`Error getting/creating collection '${collectionName}':`, error);
throw error;
}
}
async listAllCollections() {
try {
const collections = await this.database.listCollections().toArray();
console.log(`Found ${collections.length} collections:`);
collections.forEach(collection => {
console.log(` - ${collection.name}`);
});
return collections;
} catch (error) {
console.error("Error listing collections:", error);
throw error;
}
}
async close() {
try {
await this.client.close();
console.log("Connection closed");
} catch (error) {
console.error("Error closing connection:", error);
}
}
}
// Usage example
async function main() {
const connectionString = "mongodb://localhost:27017/";
const dbName = "mydb";
const collectionManager = new CollectionManager(connectionString, dbName);
try {
const connected = await collectionManager.connect();
if (!connected) return;
// Create a regular collection
await collectionManager.createCollection("users");
// Create a collection with validation
await collectionManager.createCollection("products", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "price"],
properties: {
name: { bsonType: "string" },
price: { bsonType: "double", minimum: 0 }
}
}
}
});
// Get or create collection (idempotent)
const customersCollection = await collectionManager.getOrCreateCollection("customers");
// List all collections
await collectionManager.listAllCollections();
console.log("Collection setup completed successfully!");
} catch (error) {
console.error("Main function error:", error);
} finally {
await collectionManager.close();
}
}
// Run the example
main();